import groovy.sql.Sql

public class Pacx {

    def config, dbuser, dbpasswd, dbname, output_dir, log, extractionDir, dbdriver, sdf, sql, queries
    
    public Pacx() {
        super()
        sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        config = new ConfigSlurper().parse(new File('Configuration.groovy').toURL())
        dbuser = config.doc4.database.user
        dbpasswd = config.doc4.database.password
        dbname = config.doc4.database.name
        extractionDir = config.doc4.extraction.outputdir
        dbdriver = config.doc4.database.driver
        sql = Sql.newInstance(dbname, dbuser, dbpasswd, dbdriver)    
    }
    
    def int stringToInteger(String str) {
        
        //SQL: floor(length(package)/ASCII(package)*1000)
        int length = str.length()
        int code = (int)str.substring(0,1)
        int value = Math.floor(length/code * 1000)
        return value
    
    }
    
    def void log(String message) {
        def sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm")
        message = sdf.format(new Date())+" "+message
        println message
        log.append("$message\n")
    }
    
    
    def void saveWebPage(String address, String filePath) { 
        def file = new FileOutputStream(filePath)
        def out = new BufferedOutputStream(file)
        out << new URL(address).openStream()
        out.close()
    }

}

